<?php
declare(strict_types=1);

/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-03-10
 * Time: 16:58:06
 * Info: 后台日志
 */

namespace app\admin\controller;

use think\facade\View;
use app\common\model\LoginLog as LoginLogModel;
use app\common\model\SystemLog as SystemLogModel;
use think\response\View as ViewResponse;

class AdminLog extends Base
{

    // 常量定义
    private const DELETE_TYPE_ALL = 1;
    private const DELETE_TYPE_OLD = 2;
    private const OLD_LOG_DAYS = 30;

    /**
     * 后台登录日志
     *
     * @return string|ViewResponse
     */
    public function loginLog()
    {
        if ($this->request->isAjax()) {
            $param = [
                'page'  => (int)$this->request->param('page', 1),
                'limit' => (int)$this->request->param('limit', 10),
                'key'   => $this->request->param('key', ''),
            ];

            $data = LoginLogModel::getLogList($param);
            $this->success('ok', $data['list'], (int)$data['count']);
        }

        return View::fetch();
    }

    /**
     * 删除登录日志
     *
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function loginLogDel(): void
    {
        $this->validateRequest();

        $type = (int)$this->request->param('type', 0);

        switch ($type) {
            case self::DELETE_TYPE_ALL:
                $this->deleteAllLoginLogs();
                break;
            case self::DELETE_TYPE_OLD:
                $this->deleteOldLoginLogs();
                break;
            default:
                $this->error(__('ParamError'));
        }

        $this->success(__('OperationSuccess'));
    }

    /**
     * 后台操作日志
     *
     * @return string|ViewResponse
     */
    public function systemLog()
    {
        if ($this->request->isAjax()) {
            $param = [
                'page'  => (int)$this->request->param('page', 1),
                'limit' => (int)$this->request->param('limit', 10),
                'key'   => $this->request->param('key', ''),
            ];

            $data = SystemLogModel::getLogList($param);
            $this->success('ok', $data['list'], (int)$data['count']);
        }

        return View::fetch();
    }

    /**
     * 删除系统操作日志
     *
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function systemLogDel(): void
    {
        $this->validateRequest();

        $type = (int)$this->request->param('type', 0);

        switch ($type) {
            case self::DELETE_TYPE_ALL:
                $this->deleteAllSystemLogs();
                break;
            case self::DELETE_TYPE_OLD:
                $this->deleteOldSystemLogs();
                break;
            default:
                $this->error(__('ParamError'));
        }

        $this->success(__('OperationSuccess'));
    }

    /**
     * 验证请求
     *
     * @return void
     */
    private function validateRequest(): void
    {
        if ( ! $this->request->isAjax()) {
            $this->error(__('RequestMethodError'));
        }
    }

    /**
     * 删除所有登录日志
     *
     * @return void
     * @throws \think\db\exception\DbException
     */
    private function deleteAllLoginLogs(): void
    {
        LoginLogModel::where("1=1")->delete();
    }

    /**
     * 删除旧登录日志（30天前）
     *
     * @return void
     * @throws \think\db\exception\DbException
     */
    private function deleteOldLoginLogs(): void
    {
        $lastTime = time() - 3600 * 24 * self::OLD_LOG_DAYS;
        LoginLogModel::whereTime('create_time', '<', $lastTime)->delete();
    }

    /**
     * 删除所有系统日志
     *
     * @return void
     * @throws \think\db\exception\DbException
     */
    private function deleteAllSystemLogs(): void
    {
        SystemLogModel::where("1=1")->delete();
    }

    /**
     * 删除旧系统日志（30天前）
     *
     * @return void
     * @throws \think\db\exception\DbException
     */
    private function deleteOldSystemLogs(): void
    {
        $lastTime = time() - 3600 * 24 * self::OLD_LOG_DAYS;
        SystemLogModel::whereTime('create_time', '<', $lastTime)->delete();
    }
}
